package com.xiaotu.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaotu.constant.CrowdConstant;
import com.xiaotu.entity.Admin;
import com.xiaotu.entity.AdminExample;
import com.xiaotu.exception.EditUserFaildException;
import com.xiaotu.exception.LoginFailedException;
import com.xiaotu.mapper.AdminMapper;
import com.xiaotu.service.AdminService;
import com.xiaotu.util.CrowdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public void saveAdmin(Admin admin) {
        //密码加密
        String md5 = CrowdUtil.md5(admin.getUserPswd());
        admin.setUserPswd(md5);
        //生成创建时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        String date = simpleDateFormat.format(new Date());
        admin.setCreateTime(date);
        try{
            adminMapper.insert(admin);
        }catch (Exception e){
            e.printStackTrace();
            if(e instanceof DuplicateKeyException){
                throw new EditUserFaildException(CrowdConstant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
            }
        }
    }
    @Override
    public List<Admin> getAll() {

        return adminMapper.selectByExample(new AdminExample());
    }
    @Override
    public Admin getAdminByLoginAcct(String loginAcct, String userPswd) {
        //根据登录账号查询admin对象
            //1 创建AdminExample对象
            AdminExample adminExample = new AdminExample();
            //2创建Criteria对象
        AdminExample.Criteria criteria = adminExample.createCriteria();
        //在criteria对象中封装查询条件
        criteria.andLoginAcctEqualTo(loginAcct);
            //调用AdminMapper的方法执行查询\
        List<Admin> adminList = adminMapper.selectByExample(adminExample);
        // 判断Admin对象是否为空
        if(adminList == null || adminList.size() == 0){
            throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);
        }
        if (adminList.size()>1){
            throw new LoginFailedException(CrowdConstant.MESSAGE_SYSTEM_ERROR_LOGIN_NOT_UNIQUE);
        }
        Admin admin = adminList.get(0);
        //如果Admin对象为空则抛出异常
        if(admin == null){
            throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);
        }
        //如果Admin对象不为null 则将数据库密码从Admin对象中取出
        String userPswdDB = admin.getUserPswd();
        //将表单提交的明文密码进行加密
        String userPswdForm = CrowdUtil.md5(userPswd);
        //将密码进行比较,如果比较结果不一致则抛出异常
        if(!Objects.equals(userPswdDB, userPswdForm)){
            throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);
        }
        //如果一致则返回Admin对象
        return admin;
    }
    @Override
    public PageInfo<Admin> getPageInfo(String keyword, Integer pageNum, Integer pageSize) {
        //先调用pageHepler的静态方法,开启分页功能
        PageHelper.startPage(pageNum, pageSize);
        //执行查询
        List<Admin> adminList = adminMapper.selectAdminByKeyword(keyword);
        //封装到pageInfo
        PageInfo<Admin> adminPageInfo = new PageInfo<>(adminList);
        //System.out.println(adminPageInfo);
        return adminPageInfo;
    }

    @Override
    public void removeAdminId(Integer adminId) {
        adminMapper.deleteByPrimaryKey(adminId);
    }

    @Override
    public Admin getAdminByAdminId(Integer adminId) {
        return adminMapper.selectByPrimaryKey(adminId);
    }
    @Override
    public void update(Admin admin) {

        try {
            adminMapper.updateByPrimaryKeySelective(admin);
        }catch (Exception e){
            if(e instanceof DuplicateKeyException){
                throw new EditUserFaildException(CrowdConstant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
            }
        }
    }
}
